﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MarvinsArena.Core;
using MarvinsArena.Core;

namespace BattleLogic
{
	public class Map:IMap
	{
		private Random rand = new Random();
		private List<Vector2> robotSpawnPlaces = new List<Vector2>();

		public int[,] Heightmap { get; set; }
		public int MapHeight { get { return this.Heightmap.GetLength(1) - 2; } }
		public int MapWidth { get { return this.Heightmap.GetLength(0) - 2; } }
		public int HeightmapLength0 { get { return this.Heightmap.GetLength(0); } }
		public int HeightmapLength1 { get { return this.Heightmap.GetLength(1); } }
		public float MapScale { get { return 32.0f; } }

		public Map(TournamentMap map)
		{
            this.Heightmap = new int[map.MapWidth + 2, map.MapHeight + 2];
			for (int i = 0; i < map.MapWidth + 2; i++)
			{
				for (int j = 0; j < map.MapHeight + 2; j++)
				{
                    if (i == 0 || j == 0 || i == map.MapWidth + 1 || j == map.MapHeight + 1)
                        this.Heightmap[i, j] = 1;
                    else
                        this.Heightmap[i, j] = map[i - 1, j - 1];
					
				}
			}

            this.BuildNewSpawnList();
		}

		public Map(int mapSize)
			: this(mapSize, mapSize)
		{
		}

		public Map(int widht, int height)
		{
			this.Heightmap = new int[widht + 2, height + 2];
			for (int i = 0; i < widht + 2; i++)
			{
				for (int j = 0; j < height + 2; j++)
				{
					if (i == 0 || j == 0 || i == widht + 1 || j == height + 1)
						this.Heightmap[i, j] = 1;
				}
			}
			this.BuildNewSpawnList();
		}

		private  void BuildNewSpawnList()
		{
			// Set Spawn places to center of map nodes
			robotSpawnPlaces.Clear();

            for (int i = 0; i < HeightmapLength0; i++)
                for (int j = 0; j < HeightmapLength1; j++)
                    if (this.Heightmap[i, j] == 0)
                        robotSpawnPlaces.Add(
                            new Vector2(
                                i * this.MapScale + this.MapScale / 2,
                                j * this.MapScale + this.MapScale / 2
                                )
                            );
		}

		public void PlaceRobot(IRobot robot)
		{
			int r = rand.Next(robotSpawnPlaces.Count);
			robot.PositionX = robotSpawnPlaces[r].X;
			robot.PositionY = robotSpawnPlaces[r].Y;
			robotSpawnPlaces.Remove(robotSpawnPlaces[r]);

			robot.Rotation = rand.Next(0, (int)(Math.PI * 2));
			robot.RotationGun = robot.Rotation;
			robot.RotationRadar = robot.Rotation;
		}

		public bool CheckCollision(IWorldObject obj)
		{
			for (int x = -1; x < this.HeightmapLength0 - 1; x++)
			{
				int mapX = (int)(x * this.MapScale);
				for (int y = -1; y < this.HeightmapLength1 - 1; y++)
				{
					int mapY = (int)(y * this.MapScale);

					if (this.Heightmap[x + 1, y + 1] == 1 &&
						(obj.PositionX - obj.Radius <= mapX + this.MapScale) &&
						(obj.PositionX + obj.Radius >= mapX) &&
						(obj.PositionY - obj.Radius <= mapY + this.MapScale) &&
						(obj.PositionY + obj.Radius >= mapY))
						return true;
				}
			}

			return false;
		}
	}
}
